/**
 * @file flash_main.h
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#ifndef __FLASH_MAIN_H
#define __FLASH_MAIN_H

#ifdef __cplusplus
    extern "C" {
#endif

#include "global.h"
#include "aadl/flash/config.h"

/** Flash Param */
#define SPARA_SIZE      	(256)
#define PAGE_SIZE       	(256)

/** 1. 1st sector param`s list param */
/** 1.1 fota flag */
#define FOTA_MARK_OFFSET    (0)
#define FOTA_MARK_MAX_SIZE  (8)

#define SEC_SIZE			0x1000		/// 4K SIZE
#define K1_SIZE			    0x0400		/// 1K SIZE



/**********  ZONE  ID DEFINE **********************/

/** FILE TYPE ZONE**/
#define FILE_OTA_FLAG_ID			100
#define FILE_OTA_ID					101
#define FILE_AGPS_OFF_ID			102
#define FILE_AGPS_ON_ID				103
//#define FILE_CONFIG_ID				104
#define FILE_RESET_FLAG_ID			105
//#define FILE_NEW_CONFIG_ID			106
#define FILE_CONFIG_ID              107

#if 1 //def SSL_ENABLE
#define FILE_SSL_CERT_FILE_UPD_ID   125  /* SSL Cert files upd zone*/

#define FILE_CA_FILE_ID             126  /* CA file */
#define FILE_CLIENT_CRT_ID          127  /* Client certificate file ---- ScooterCert.crt */
#define FILE_CLIENT_KEY_ID          128  /* Client private key file ---- ScooterCertPrivateKey.key */
#endif

//L3模式库文件
#define FILE_L3_LIB_ID              129

/** DISCRETE TYPE ZONE**/
#define DISCRETE_DIS_ID				201

#define FATAL_PARA_ID               301
#define FATAL_PARA_ID2              302
#define FATAL_PARA_ID3              303
#define FATAL_PARA_ID4              304


/** FREQ TYPE ZONE**/
#define FREQ_GPSDATA_ID			    401
#define FREQ_SREIAL_NUM_ID			402        
#define FREQ_AGPS_ON_ID			    403
#define FREQ_AGPS_OFF_ID			404
#define FREQ_CONFIG_ID				405
#define FREQ_TIME_ID				406
#define FREQ_DOG_FLAG_ID			407           /* DOG标志 */
//#define FREQ_METER_ID			    408           /* 里程统计*/
#define FREQ_ONEWIRE_ID             408           /* ONEWIRE排序 */
#define FREQ_HMC_ID                 409
#define FREQ_MILEAGE_ID             410           /* 里程统计 */
#define FREQ_RTS_ID                 411
#define FREQ_MISC_DATA_ID           412
#define FREQ_RMD_DATA_ID            413
#define FREQ_STT_DATA_ID			414
#define FREQ_IDL_DATA_ID			415
#define FREQ_EFS_DATA_ID            422	  
#define FREQ_IDA_DATA_ID			416


/** DIRTECT TYPE ZONE**/
#define DIRECT_RETAIN_ID            500
#define PWR_OFF_FLAG_ID             501
#define PWR_OFF_FLAG_OFFSET         0
#define CAN_SYN_FLAG_OFFSET         4

#ifdef TELCEL_ENABLE
#define TELCEL_DATA_OFFSET          48
#endif

#define FREQ_BRAKE_ID               502
#define FREQ_STABLE_DATA_ID         503
#define FREQ_CALIB_COEFF_ID         504

/** CIRCULAR TYPE ZONE**/
#define QUEUE_EBUF_ID			    601           /*正常事件队列 -------------*/  
#define QUEUE_HBUF_ID	            602           /*高优先事件队列 -----------*/
#define QUEUE_RECORD_ID			    603           /*历史记录缓冲区 -----------*/
#define QUEUE_SMS_BUF_ID            604           /*短信缓冲区 ---------------*/
#define QUEUE_UNK_ROA_ID            605           /* 未知漫游缓冲区 BUFFER区  */
 
	  
#ifdef DMS_SERVER_USE
    /****************************************************************
                 DMS 相关存储Flash的ID号以及大小定义
    ****************************************************************/
#define CIRCULAR_DMS_EBUF_ID		610
#define CIRCULAR_DMS_HBUF_ID		611
#define CIRCULAR_DMS_REC_ID			612
#define CIRCULAR_DMS_SMS_ID			613
#define FREQ_DMS_SREIAL_NUM_ID      614
#define FZONE_DISCRETE_DMS_ID       615  
/***************************************************************/

#define CIRCULAR_DMS_EBUF_SIZE          K1_SIZE*16
#define CIRCULAR_DMS_HBUF_SIZE          K1_SIZE*16
#define CIRCULAR_DMS_REC_SIZE           K1_SIZE*32
#define CIRCULAR_DMS_SMS_SIZE           K1_SIZE*16
#define FREQ_DMS_SERIAL_NUM_TOTAL_SIZE  K1_SIZE*4
#define FREQ_VER_RECORD_TOTAL_SIZE      K1_SIZE*4
#endif


//smartX L3
#define SMART_L3_PATTERN_LIB_ID       616
#define SAMRT_L3_PATTERN_LIB_SIZE     K1_SIZE*4


/*****************************************************/
/* AADL库规定 */
/*第0个扇区必须是direct类型，且大小为4096*/
/*第1个扇区必须是file类型*/

#define FILE_OTA_FLAG_SIZE				K1_SIZE*4  
#define FILE_OTA_SIZE                   K1_SIZE*1012
#define FILE_AGPS_OFF_SIZE				K1_SIZE*28
#define FILE_AGPS_ON_SIZE				K1_SIZE*8
#define FILE_CONFIG_SIZE				K1_SIZE*8
#define FILE_NEW_CONFIG_SIZE			K1_SIZE*48

#define SSL_FILE_SIZE                   (K1_SIZE*8)
#define SSL_FILE_UPD_SIZE               (K1_SIZE*48)  //ssl file size X 3

#define NORMAL_RESET_FLAG_SIZE          K1_SIZE*4

#define DISCRETE_DIS_SIZE				K1_SIZE*16
#define FATAL_PARA_SIZE					K1_SIZE*128

#define FREQ_GPSDATA_TOTAL_SIZE		    K1_SIZE*8
        
#define FREQ_SERIAL_NUM_TOTAL_SIZE		K1_SIZE*64
        
#define FREQ_AGPS_ON_TOTAL_SIZE		    K1_SIZE*8
#define FREQ_AGPS_OFF_TOTAL_SIZE		K1_SIZE*8
#define FREQ_CONFIG_TOTAL_SIZE		    K1_SIZE*8
#define FREQ_TIME_TOTAL_SIZE			K1_SIZE*8
#define FREQ_DOG_FLAG_SIZE			    K1_SIZE*8
#define FREQ_ONEWIRE_SIZE			    K1_SIZE*8
#define FREQ_HMC_SIZE			        K1_SIZE*8
#define FREQ_MILEAGE_SIZE	            K1_SIZE*8
#define FREQ_RTS_SIZE	                K1_SIZE*8
#define FREQ_MISC_DATA_SIZE             K1_SIZE*8
#define	K8_SIZE							K1_SIZE*8
#define QUEUE_EBUF_SIZE				    K1_SIZE * (64 + 20)     // 20 + 64 KB
#define QUEUE_HBUF_SIZE                 K1_SIZE * (32 + 16)     // 16 + 32 KB
#define QUEUE_RECORD_SIZE               K1_SIZE * (1024)         // 8MB
#define QUEUE_SMS_BUF_SIZE              K1_SIZE * (32 + 16)


#define FILE_L3_LIB_SIZE                K1_SIZE * 4  //暂定模式库文件大小为4k

#define CMD_READ_ID		0x9F      
typedef struct {
	unsigned char flash_manufacture;
	unsigned char flash_type;
	unsigned char flash_capacity;
}flash_id_t;      
      
int32_t flash_main_init(void);
void flash_task_entry (void);

/*------------------------direct zone operate ----------------*/
typedef enum {
    DRET_ID_OTA = 0,
    DRET_ID_PIN,
    DRET_ID_TEST,
}direct_id_e;

extern int32_t direct_retain_write(direct_id_e id, void * buf, int32_t num);
extern int32_t direct_retain_read(direct_id_e id, void * buf, int32_t num);

#ifdef __cplusplus
    }
#endif

#endif

